package pm.user.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import pm.db.DBUtil;
import pm.db.dbConnection;
import pm.user.bean.UserBean;

public class UserService {

	public UserBean showUserInfo(String username) {
		UserBean userbean = new UserBean();
		String sql = "select * from pm_user where username = ?";
		Connection conn = DBUtil.getConnection();
		PreparedStatement past = null;
		try {
			past = conn.prepareStatement(sql);
			past.setString(1, username);
			ResultSet rs = past.executeQuery();
			if (!rs.next()) {
				return null;
			}
			userbean.setUsername(rs.getString("username"));
			userbean.setPassword(rs.getString("password"));
			userbean.setName(rs.getString("name"));
			userbean.setPhone(rs.getString("phone"));
			userbean.setRole(rs.getString("role"));

			rs.close();
			past.close();
			DBUtil.close(conn);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {

		}
		return userbean;
	}

	public boolean modifyUserInfo(UserBean user) {
		String sql1 = null;

		sql1 = " update pm_user set phone=" + "'" + user.getPhone() + "'"
				+ ", " + "name=" + "'" + user.getName() + "'"
				+ ", " + "role=" + "'" + user.getRole()+ "'"
				+ "where username =" + "'" + user.getUsername() + "'";

		Connection conn = DBUtil.getConnection();
		PreparedStatement ps1 = null;

		try {
			ps1 = conn.prepareStatement(sql1);

			ps1.executeUpdate();
			ps1.close();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			DBUtil.close(conn);
		}
		return true;
	}
	public boolean modifyOneUser(UserBean user) {
		String sql1 = null;

		sql1 = " update pm_user set phone=" + "'" + user.getPhone() + "'"
				+ ", " + "name=" + "'" + user.getName() + "'"
				+ ", " + "role=" + "'" + user.getRole() + "'"
				+ "where username =" + "'" + user.getUsername() + "'";

		Connection conn = DBUtil.getConnection();
		PreparedStatement ps1 = null;

		try {
			ps1 = conn.prepareStatement(sql1);

			ps1.executeUpdate();
			ps1.close();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			DBUtil.close(conn);
		}
		return true;
	}

	public boolean modifyPsw(UserBean user) {
		String sql1 = null;

		sql1 = " update pm_user set password=" + "'" + user.getPassword() + "'"
				+ "where username =" + "'" + user.getUsername() + "'";

		Connection conn = DBUtil.getConnection();
		PreparedStatement ps1 = null;

		try {
			ps1 = conn.prepareStatement(sql1);

			ps1.executeUpdate();
			ps1.close();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			DBUtil.close(conn);
		}
		return true;
	}

	public List showAllUser() {
		List<UserBean> list = new ArrayList<UserBean>();

		String sql1 = "select * from pm_user";

		PreparedStatement past = null;
		Connection conn = DBUtil.getConnection();
		ResultSet rs = null;

		try {
			past = conn.prepareStatement(sql1);

			rs = past.executeQuery();
			while (rs.next()) {
				UserBean user = new UserBean();

				user.setUsername(rs.getString("username"));
				user.setName(rs.getString("name"));
				user.setPhone(rs.getString("phone"));
				user.setRole(rs.getString("role"));
				list.add(user);
			}
			rs.close();
			past.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn);
		}
		return list;
	}

	 public boolean deleteuser(String username) {
	 boolean flag = false;
	 Connection conn = DBUtil.getConnection();
	 String sql = "delete from pm_user where username='"
	 + username + "'";
	 PreparedStatement past = null;
	 try {
	 past = conn.prepareStatement(sql);
	 past.executeUpdate();
	 past.close();
	 conn.close();
	 flag = true;
	 } catch (SQLException e) {
	 e.printStackTrace();
	 } finally {
		 DBUtil.close(conn);
	 }
	 return flag;
	 }

	public boolean addOneUser(UserBean user) {
		boolean flag = false;
		Connection conn = DBUtil.getConnection();
		String sql = "insert into pm_user(username,password,phone,name,role,salt) values("
				+ "'"
				+ user.getUsername()
				+ "'"
				+ ","
				+ "'"
				+ user.getPassword()
				+ "'"
				+ ","

				+ "'"
				+ user.getPhone()
				+ "'"
				+ ","
				+ "'"
				+ user.getName()
				+ "'"
				+ ","
				+ "'"
				+ user.getRole()
				+ "'"
				+ ","
				+ "'"
				+ user.getSalt() + "'" + ")";
		PreparedStatement past = null;
		try {
			past = conn.prepareStatement(sql);
			past.executeUpdate();
			past.close();
			conn.close();
			flag = true;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn);
		}

		return flag;
	}

	public String getSalt(UserBean user) {
		String salt = "";
		UserBean userbean = new UserBean();
		String sql = "select salt from pm_user where username = ?";
		Connection conn = DBUtil.getConnection();
		PreparedStatement past = null;
		try {
			past = conn.prepareStatement(sql);
			past.setString(1, user.getUsername());
			ResultSet rs = past.executeQuery();
			if (!rs.next()) {
				return null;
			}

			salt = rs.getString("salt");
			System.out.println("salt in service-->" + salt);
			rs.close();
			past.close();
			DBUtil.close(conn);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {

		}

		return salt;
	}

}
